//
//  RootViewController.m
//  04 为图片添加滤镜
//
//  Created by dllo on 16/3/14.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "RootViewController.h"

#import "ShowFilterCollectionViewCell.h"

#import "GPUImage.h"

@interface RootViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

// 保存滤镜的数组
@property(nonatomic, strong)NSMutableArray *filterArr;

// 保存添加滤镜后的图片
@property(nonatomic, strong)NSMutableArray *filterImageArr;

// 保存要添加滤镜的图片
@property(nonatomic, strong)UIImage *inputImage;
// 保存添加滤镜后的图片
@property(nonatomic, strong)UIImage *filterImage;



// 显示图片的Button的对象
@property (strong, nonatomic) IBOutlet UIButton *imageButton;
// 显示滤镜效果
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;

// 添加滤镜
- (IBAction)addFilter:(id)sender;
// 显示从本地取出的图片
- (IBAction)imageButton:(id)sender;
// 快速的创建一个滤镜
- (IBAction)quickFilter:(id)sender;
// 重新生成滤镜图片
- (IBAction)reloadFilterImage:(id)sender;
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    // 初始化需要添加滤镜的图片
    self.inputImage = [UIImage imageNamed:@"1"];
    
    // 创建视图
    [self createView];
    
    // 创建滤镜数组
    [self createFilterArr];
    
    // 为图片添加滤镜
    [self createImageFilter];
    
}

#pragma mark 创建滤镜数组
- (void)createFilterArr{
    // 初始化
    self.filterArr = [NSMutableArray array];
    
    // 0 亮度
    GPUImageBrightnessFilter *imageBrightnessFilter = [[GPUImageBrightnessFilter alloc] init];
    [self.filterArr addObject:imageBrightnessFilter];
    
    // 1 曝光
   GPUImageExposureFilter *imageExposureFilter = [[GPUImageExposureFilter alloc] init];
    [self.filterArr addObject:imageExposureFilter];
    
    // 2 对比度
    GPUImageContrastFilter *imageContrastFilter = [[GPUImageContrastFilter alloc] init];
    [self.filterArr addObject:imageContrastFilter];
    
    // 3 饱和度
    GPUImageSaturationFilter *imageSaturationFilter = [[GPUImageSaturationFilter alloc] init];
    [self.filterArr addObject:imageSaturationFilter];

    // 4 伽马线
    GPUImageGammaFilter *imageGammaFilter = [[GPUImageGammaFilter alloc] init];
    [self.filterArr addObject:imageGammaFilter];
    
    // 5 反色
    GPUImageColorInvertFilter *imageColorInvertFilter = [[GPUImageColorInvertFilter alloc] init];
    [self.filterArr addObject:imageColorInvertFilter];
    
    // 6 褐色(怀旧)
    GPUImageSepiaFilter *imageSepiaFilter = [[GPUImageSepiaFilter alloc] init];
    [self.filterArr addObject:imageSepiaFilter];
    
    // 7 色阶
    GPUImageLevelsFilter *imageLevelsFilter = [[GPUImageLevelsFilter alloc] init];
    [self.filterArr addObject:imageLevelsFilter];
    
    // 8 灰度
    GPUImageGrayscaleFilter *imageGrayscaleFilter = [[GPUImageGrayscaleFilter alloc] init];
    [self.filterArr addObject:imageGrayscaleFilter];
    
    // 9 色彩直方图,显示在图像上
    GPUImageHistogramFilter *imageHistogramFilter = [[GPUImageHistogramFilter alloc] init];
    [self.filterArr addObject:imageHistogramFilter];
    
    // 10 色彩直方图
    GPUImageHistogramGenerator *imageHistogramGeneratorFilter = [[GPUImageHistogramGenerator alloc] init];
    [self.filterArr addObject:imageHistogramGeneratorFilter];
    
    // 11 RGB
    GPUImageRGBFilter *imageRGBFilter = [[GPUImageRGBFilter alloc] init];
    [self.filterArr addObject:imageRGBFilter];

    // 12 色调曲线
    GPUImageToneCurveFilter *imageToneCurveFilter = [[GPUImageToneCurveFilter alloc] init];
    [self.filterArr addObject:imageToneCurveFilter];
    
    // 13 单色
    GPUImageMonochromeFilter *imageMonochromeFilter = [[GPUImageMonochromeFilter alloc] init];
    [self.filterArr addObject:imageMonochromeFilter];
    
    // 14 不透明
    GPUImageOpacityFilter *imageOpacityFilter = [[GPUImageOpacityFilter alloc] init];
    [self.filterArr addObject:imageOpacityFilter];
    
    // 15 提亮阴影
    GPUImageHighlightShadowFilter *imageHighlightShadowFilter = [[GPUImageHighlightShadowFilter alloc] init];
    [self.filterArr addObject:imageHighlightShadowFilter];
    
    // 16 色彩替换(替换亮部和暗部的色彩)
    GPUImageFalseColorFilter *imageFalseColorFilter = [[GPUImageFalseColorFilter alloc] init];
    [self.filterArr addObject:imageFalseColorFilter];
    // 17 色度
    GPUImageHueFilter *imageHueFilter = [[GPUImageHueFilter alloc] init];
    [self.filterArr addObject:imageHueFilter];
    
    // 18 色度键
    GPUImageChromaKeyFilter *imageChromaKeyFilter = [[GPUImageChromaKeyFilter alloc] init];
    [self.filterArr addObject:imageChromaKeyFilter];
    
    // 19 白平衡
    GPUImageWhiteBalanceFilter *imageWhiteBalanceFilter = [[GPUImageWhiteBalanceFilter alloc] init];
    [self.filterArr addObject:imageWhiteBalanceFilter];
    
    // 20 像素平均色值
    GPUImageAverageColor *imageAverageFilter = [[GPUImageAverageColor alloc] init];
    [self.filterArr addObject:imageAverageFilter];
    
    // 21 纯色
    GPUImageSolidColorGenerator *imageSolidColorGeneratorFilter = [[GPUImageSolidColorGenerator alloc] init];
    [self.filterArr addObject:imageSolidColorGeneratorFilter];
    
    // 22 亮度平均
    GPUImageLuminosity *imageLuminosityFilter = [[GPUImageLuminosity alloc] init];
    [self.filterArr addObject:imageLuminosityFilter];
    
    // 23 像素色值亮度平均，图像黑白（有类似漫画效果）
    GPUImageAverageLuminanceThresholdFilter *imageAverageLuminanceThresholdFilter = [[GPUImageAverageLuminanceThresholdFilter alloc] init];
    [self.filterArr addObject:imageAverageLuminanceThresholdFilter];
    
    // 24 lookup 色彩调整
    GPUImageLookupFilter *imageLookupFilter = [[GPUImageLookupFilter alloc] init];
    [self.filterArr addObject:imageLookupFilter];
    
    // 25 Amatorka lookup
    GPUImageAmatorkaFilter *imageAmatorkaFilter = [[GPUImageAmatorkaFilter alloc] init];
    [self.filterArr addObject:imageAmatorkaFilter];
    
    // 26 MissEtikate lookup
    GPUImageMissEtikateFilter *imageMissEtikateFilter = [[GPUImageMissEtikateFilter alloc] init];
    [self.filterArr addObject:imageMissEtikateFilter];
    
    
    // 27 SoftElegance lookup
    GPUImageSoftEleganceFilter *imageSoftEleganceFilter = [[GPUImageSoftEleganceFilter alloc] init];
    [self.filterArr addObject:imageSoftEleganceFilter];
    
#pragma mark 图像处理 Handle Image
    
    
    // 28 十字
    GPUImageCrosshairGenerator *imageCrosshairGeneratorFilter = [[GPUImageCrosshairGenerator alloc] init];
    [self.filterArr addObject:imageCrosshairGeneratorFilter];
    
    // 29 线条
    GPUImageLineGenerator *imageLineGeneratorFilter = [[GPUImageLineGenerator alloc] init];
    [self.filterArr addObject:imageLineGeneratorFilter];
    
    // 30 形状变化
    GPUImageTransformFilter *imageTransformFilter = [[GPUImageTransformFilter alloc] init];
    [self.filterArr addObject:imageTransformFilter];
    
    // 31 剪裁
    GPUImageCropFilter *imageCropFilter = [[GPUImageCropFilter alloc] init];
    [self.filterArr addObject:imageCropFilter];
    
    // 32 锐化
    GPUImageSharpenFilter *imageSharpenFilter = [[GPUImageSharpenFilter alloc] init];
    [self.filterArr addObject:imageSharpenFilter];
    
    // 33 反遮罩锐化
    GPUImageUnsharpMaskFilter *imageUnsharpMaskFilter = [[GPUImageUnsharpMaskFilter alloc] init];
    [self.filterArr addObject:imageUnsharpMaskFilter];
    
    // 34 模糊
//    GPUImageFastBlurFilter *imageFastBlurFilter = [[GPUImageFastBlurFilter alloc] init];
//    [self.filterArr addObject:imageFastBlurFilter];
    
    // 35 高斯模糊
    GPUImageGaussianBlurFilter *imageGaussianBlurFilter = [[GPUImageGaussianBlurFilter alloc] init];
    [self.filterArr addObject:imageGaussianBlurFilter];
    
    // 36 高斯模糊，选择部分清晰
    GPUImageGaussianSelectiveBlurFilter *imageGaussianSelectiveBlurFilter = [[GPUImageGaussianSelectiveBlurFilter alloc] init];
    [self.filterArr addObject:imageGaussianSelectiveBlurFilter];
    
    // 37 盒状模糊
    GPUImageBoxBlurFilter *imageBoxBlurFilter = [[GPUImageBoxBlurFilter alloc] init];
    [self.filterArr addObject:imageBoxBlurFilter];
    
    // 38 条纹模糊，中间清晰，上下两端模糊
    GPUImageTiltShiftFilter *imageTiltShiftFilter = [[GPUImageTiltShiftFilter alloc] init];
    [self.filterArr addObject:imageTiltShiftFilter];
    
    // 39 中间值，有种稍微模糊边缘的效果
    GPUImageMedianFilter *imageMedianFilter = [[GPUImageMedianFilter alloc] init];
    [self.filterArr addObject:imageMedianFilter];
    
    // 40 双边模糊
    GPUImageBilateralFilter *imageBilateralFilter = [[GPUImageBilateralFilter alloc] init];
    [self.filterArr addObject:imageBilateralFilter];
    
    // 41 侵蚀边缘模糊，变黑白
    GPUImageErosionFilter *imageErosionFilter = [[GPUImageErosionFilter alloc] init];
    [self.filterArr addObject:imageErosionFilter];
    
    // 42 RGB侵蚀边缘模糊，有色彩
    GPUImageRGBErosionFilter *imageRGBErosionFilter = [[GPUImageRGBErosionFilter alloc] init];
    [self.filterArr addObject:imageRGBErosionFilter];
    
    // 43 扩展边缘模糊，变黑白
    GPUImageDilationFilter *imageDilationFilter = [[GPUImageDilationFilter alloc] init];
    [self.filterArr addObject:imageDilationFilter];
    
    // 44 RGB扩展边缘模糊，有色彩
    GPUImageRGBDilationFilter *imageRGBDilationFilter = [[GPUImageRGBDilationFilter alloc] init];
    [self.filterArr addObject:imageRGBDilationFilter];
    
    // 45 黑白色调模糊
    GPUImageOpeningFilter *imageOpeningFilter = [[GPUImageOpeningFilter alloc] init];
    [self.filterArr addObject:imageOpeningFilter];
    
    // 46 彩色模糊
    GPUImageRGBOpeningFilter *imageRGBOpeningFilter = [[GPUImageRGBOpeningFilter alloc] init];
    [self.filterArr addObject:imageRGBOpeningFilter];
    
    // 47 黑白色调模糊，暗色会被提亮
    GPUImageClosingFilter *imageClosingFilter = [[GPUImageClosingFilter alloc] init];
    [self.filterArr addObject:imageClosingFilter];
    
    // 48 彩色模糊，暗色会被提亮
    GPUImageRGBClosingFilter *imageRGBClosingFilter = [[GPUImageRGBClosingFilter alloc] init];
    [self.filterArr addObject:imageRGBClosingFilter];
    
    // 49 Lanczos重取样，模糊效果
    GPUImageLanczosResamplingFilter *imageLanczosResamplingFilter = [[GPUImageLanczosResamplingFilter alloc] init];
    [self.filterArr addObject:imageLanczosResamplingFilter];
    
    // 50 非最大抑制，只显示亮度最高的像素，其他为黑
    GPUImageNonMaximumSuppressionFilter *imageNonMaximumSuppressionFilter = [[GPUImageNonMaximumSuppressionFilter alloc] init];
    [self.filterArr addObject:imageNonMaximumSuppressionFilter];
    
    // 51 与上相比，像素丢失更多
    GPUImageThresholdedNonMaximumSuppressionFilter *imageThresholdedNonMaximumSuppressionFilter = [[GPUImageThresholdedNonMaximumSuppressionFilter alloc] init];
    [self.filterArr addObject:imageThresholdedNonMaximumSuppressionFilter];
    
    // 52 Sobel边缘检测算法(白边，黑内容，有点漫画的反色效果)
    GPUImageSobelEdgeDetectionFilter *imageSobelEdgeDetectionFilter = [[GPUImageSobelEdgeDetectionFilter alloc] init];
    [self.filterArr addObject:imageSobelEdgeDetectionFilter];
    
    // 53 Canny边缘检测算法（比上更强烈的黑白对比度）
    GPUImageCannyEdgeDetectionFilter *imageCannyEdgeDetectionFilter = [[GPUImageCannyEdgeDetectionFilter alloc] init];
    [self.filterArr addObject:imageCannyEdgeDetectionFilter];
    
    // 54 阈值边缘检测（效果与上差别不大）
    GPUImageThresholdEdgeDetectionFilter *imageThresholdEdgeDetectionFilter = [[GPUImageThresholdEdgeDetectionFilter alloc] init];
    [self.filterArr addObject:imageThresholdEdgeDetectionFilter];
    
    // 55 普瑞维特(Prewitt)边缘检测(效果与Sobel差不多，貌似更平滑)
    GPUImagePrewittEdgeDetectionFilter *imagePrewittEdgeDetectionFilter = [[GPUImagePrewittEdgeDetectionFilter alloc] init];
    [self.filterArr addObject:imagePrewittEdgeDetectionFilter];
    
    // 56 XYDerivative边缘检测，画面以蓝色为主，绿色为边缘，带彩色
    GPUImageXYDerivativeFilter *imageXYDerivativeFilter = [[GPUImageXYDerivativeFilter alloc] init];
    [self.filterArr addObject:imageXYDerivativeFilter];
    
    // 57 Harris角点检测，会有绿色小十字显示在图片角点处
    GPUImageHarrisCornerDetectionFilter *imageHarrisCornerDetectionFilter = [[GPUImageHarrisCornerDetectionFilter alloc] init];
    [self.filterArr addObject:imageHarrisCornerDetectionFilter];
    
    // 58 Noble角点检测，检测点更多
    GPUImageNobleCornerDetectionFilter *imageNobleCornerDetectionFilter = [[GPUImageNobleCornerDetectionFilter alloc] init];
    [self.filterArr addObject:imageNobleCornerDetectionFilter];
    
    // 59 ShiTomasi角点检测，与上差别不大
    GPUImageShiTomasiFeatureDetectionFilter *imageShiTomasiFeatureDetectionFilter = [[GPUImageShiTomasiFeatureDetectionFilter alloc] init];
    [self.filterArr addObject:imageShiTomasiFeatureDetectionFilter];
    
    // 61 动作检测
    GPUImageMotionDetector *imageMotionDetectorFilter = [[GPUImageMotionDetector alloc] init];
    [self.filterArr addObject:imageMotionDetectorFilter];
    
    // 62 线条检测
    GPUImageHoughTransformLineDetector *imageHoughTransformLineDetectorFilter = [[GPUImageHoughTransformLineDetector alloc] init];
    [self.filterArr addObject:imageHoughTransformLineDetectorFilter];
    
    // 63 平行线检测
    GPUImageParallelCoordinateLineTransformFilter *imageParallelCoordinateLineTransformFilter = [[GPUImageParallelCoordinateLineTransformFilter alloc] init];
    [self.filterArr addObject:imageParallelCoordinateLineTransformFilter];
    
    // 64 图像黑白化，并有大量噪点
    GPUImageLocalBinaryPatternFilter *imageLocalBinaryPatternFilter = [[GPUImageLocalBinaryPatternFilter alloc] init];
    [self.filterArr addObject:imageLocalBinaryPatternFilter];
    
    // 65 用于图像加亮
    GPUImageLowPassFilter *imageLowPassFilter = [[GPUImageLowPassFilter alloc] init];
    [self.filterArr addObject:imageLowPassFilter];
    
    // 66 图像低于某值时显示为黑
    GPUImageHighPassFilter *imageHighPassFilter = [[GPUImageHighPassFilter alloc] init];
    [self.filterArr addObject:imageHighPassFilter];
    
#pragma mark 视觉效果 Visual Effect
    
    // 67 素描
    GPUImageSketchFilter *imageSketchFilter = [[GPUImageSketchFilter alloc] init];
    [self.filterArr addObject:imageSketchFilter];
    
    // 68 阀值素描，形成有噪点的素描
    GPUImageThresholdSketchFilter *imageThresholdSketchFilter = [[GPUImageThresholdSketchFilter alloc] init];
    [self.filterArr addObject:imageThresholdSketchFilter];
    
    // 69 卡通效果（黑色粗线描边）
    GPUImageToonFilter *imageToonFilter = [[GPUImageToonFilter alloc] init];
    [self.filterArr addObject:imageToonFilter];
    
    // 70 相比上面的效果更细腻，上面是粗旷的画风
    GPUImageSmoothToonFilter *imageSmoothToonFilter = [[GPUImageSmoothToonFilter alloc] init];
    [self.filterArr addObject:imageSmoothToonFilter];
    
    // 71 桑原(Kuwahara)滤波,水粉画的模糊效果；处理时间比较长，慎用
    GPUImageKuwaharaFilter *imageKuwaharaFilter = [[GPUImageKuwaharaFilter alloc] init];
    [self.filterArr addObject:imageKuwaharaFilter];
    
    // 72 黑白马赛克
    GPUImageMosaicFilter *imageMosaicFilter = [[GPUImageMosaicFilter alloc] init];
    [self.filterArr addObject:imageMosaicFilter];
    
    // 73 像素化
    GPUImagePixellateFilter *imagePixellateFilter = [[GPUImagePixellateFilter alloc] init];
    [self.filterArr addObject:imagePixellateFilter];
    
    // 74 同心圆像素化
    GPUImagePolarPixellateFilter *imagePolarPixellateFilter = [[GPUImagePolarPixellateFilter alloc] init];
    [self.filterArr addObject:imagePolarPixellateFilter];
    
    // 75 交叉线阴影，形成黑白网状画面
    GPUImageCrosshatchFilter *imageCrosshatchFilter = [[GPUImageCrosshatchFilter alloc] init];
    [self.filterArr addObject:imageCrosshatchFilter];
    
    // 76 色彩丢失，模糊（类似监控摄像效果）
    GPUImageColorPackingFilter *imageColorPackingFilter = [[GPUImageColorPackingFilter alloc] init];
    [self.filterArr addObject:imageColorPackingFilter];
    
    // 77 晕影，形成黑色圆形边缘，突出中间图像的效果
    GPUImageVignetteFilter *imageVignetteFilter = [[GPUImageVignetteFilter alloc] init];
    [self.filterArr addObject:imageVignetteFilter];
    
    // 78 漩涡，中间形成卷曲的画面
    GPUImageSwirlFilter *imageSwirlFilter = [[GPUImageSwirlFilter alloc] init];
    [self.filterArr addObject:imageSwirlFilter];
    
    // 79 凸起失真，鱼眼效果
    GPUImageBulgeDistortionFilter *imageBulgeDistortionFilter = [[GPUImageBulgeDistortionFilter alloc] init];
    [self.filterArr addObject:imageBulgeDistortionFilter];
    
    // 80 收缩失真，凹面镜
    GPUImagePinchDistortionFilter *imagePinchDistortionFilter = [[GPUImagePinchDistortionFilter alloc] init];
    [self.filterArr addObject:imagePinchDistortionFilter];
    
    // 81 伸展失真，哈哈镜
    GPUImageStretchDistortionFilter *imageStretchDistortionFilter = [[GPUImageStretchDistortionFilter alloc] init];
    [self.filterArr addObject:imageStretchDistortionFilter];
    
    // 82 水晶球效果
    GPUImageGlassSphereFilter *imageGlassSphereFilter = [[GPUImageGlassSphereFilter alloc] init];
    [self.filterArr addObject:imageGlassSphereFilter];
    
    // 83 球形折射，图形倒立
    GPUImageSphereRefractionFilter *imageSphereRefractionFilter = [[GPUImageSphereRefractionFilter alloc] init];
    [self.filterArr addObject:imageSphereRefractionFilter];
    
    // 84 色调分离，形成噪点效果
    GPUImagePosterizeFilter *imagePosterizeFilter = [[GPUImagePosterizeFilter alloc] init];
    [self.filterArr addObject:imagePosterizeFilter];
    
    // 85 CGA色彩滤镜，形成黑、浅蓝、紫色块的画面
    GPUImageCGAColorspaceFilter *imageCGAColorspaceFilter = [[GPUImageCGAColorspaceFilter alloc] init];
    [self.filterArr addObject:imageCGAColorspaceFilter];
    
    // 86 柏林噪点，花边噪点
    GPUImagePerlinNoiseFilter *imagePerlinNoiseFilter = [[GPUImagePerlinNoiseFilter alloc] init];
    [self.filterArr addObject:imagePerlinNoiseFilter];
    
    // 87 3x3卷积，高亮大色块变黑，加亮边缘、线条等
    GPUImage3x3ConvolutionFilter *image3x3ConvolutionFilter = [[GPUImage3x3ConvolutionFilter alloc] init];
    [self.filterArr addObject:image3x3ConvolutionFilter];
    
    // 88 浮雕效果，带有点3d的感觉
    GPUImageEmbossFilter *imageEmbossFilter = [[GPUImageEmbossFilter alloc] init];
    [self.filterArr addObject:imageEmbossFilter];
    
    // 89 像素圆点花样
    GPUImagePolkaDotFilter *imagePolkaDotFilter = [[GPUImagePolkaDotFilter alloc] init];
    [self.filterArr addObject:imagePolkaDotFilter];
    
    // 90 点染,图像黑白化，由黑点构成原图的大致图形
    GPUImageHalftoneFilter *imageHalftoneFilter = [[GPUImageHalftoneFilter alloc] init];
    [self.filterArr addObject:imageHalftoneFilter];
}

#pragma mark 添加滤镜
- (void)createImageFilter{

    // 并行多线程,实现为图片添加滤镜
    dispatch_queue_t queue = dispatch_queue_create("FilterQueue", DISPATCH_QUEUE_CONCURRENT);
    self.filterImageArr = [NSMutableArray array];
    // 找到当前的工程主队列
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    
    dispatch_async(queue, ^{
        for(NSInteger i = 0; i < self.filterArr.count; i++){
            
            GPUImageOutput<GPUImageInput> *imageFilter = self.filterArr[i];
            
            UIImage *filterImage = [imageFilter imageByFilteringImage:self.inputImage];
            if(filterImage == nil){
                [self.filterImageArr addObject:[UIImage imageNamed:@"2"]];
            }
            else{
                [self.filterImageArr addObject:filterImage];
            }
            [imageFilter removeAllTargets];
        }
        // 在主队列里,刷新
        dispatch_async(mainQueue, ^{
            
            [self.collectionView reloadData];
        });
    });
}

#pragma mark 创建视图
- (void)createView{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 注册
    UINib *nib = [UINib nibWithNibName:NSStringFromClass([ShowFilterCollectionViewCell class]) bundle:nil];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:NSStringFromClass([ShowFilterCollectionViewCell class])];
}


#pragma mark 实现CollectionView的协议方法
// 个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.filterImageArr.count;
}
// 创建
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    // 设置添加滤镜后的图片
    ShowFilterCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ShowFilterCollectionViewCell class]) forIndexPath:indexPath];
    
    [cell.picImageView setImage:(UIImage *)self.filterImageArr[indexPath.row]];
    
    return cell;
}
// 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"%ld",indexPath.row);
    
    [self.imageButton setImage:(UIImage *)self.filterImageArr[indexPath.row] forState:UIControlStateNormal]; 
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)imageButton:(id)sender {
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    // 签代理
    picker.delegate = self;
    picker.allowsEditing = self;
    
    // 跳转到相册
    [self presentViewController:picker animated:YES completion:^{
    }];
}

// 实现显示图片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    // 模态跳转回来
    [picker dismissViewControllerAnimated:YES completion:^{
    }];
    
    // 将图片从字典中提取出来
    self.inputImage = [info objectForKey:UIImagePickerControllerEditedImage];
    
    // UIImagePickerControllerOriginalImage 是选择原始的
    // UIImagePickerControllerEditedImage 是编辑之后的
    [self.imageButton setImage:self.inputImage forState:UIControlStateNormal];
}
// 快速创建一个滤镜
- (IBAction)quickFilter:(id)sender {
    // 第二种方法:快速创建
    
    // 指定需要添加滤镜的图片
    if(self.inputImage == nil){
        self.inputImage = [UIImage imageNamed:@"1"];
    }
    // 创建滤镜
    GPUImageSepiaFilter *imageFilter  = [[GPUImageSepiaFilter alloc] init];
    // 添加滤镜
    UIImage *filterImage = [imageFilter imageByFilteringImage:self.inputImage];
    
    // 显示添加滤镜后的图片
    [self.imageButton setImage:filterImage forState:UIControlStateNormal];
}

- (IBAction)reloadFilterImage:(id)sender {
}

// 添加滤镜
- (IBAction)addFilter:(id)sender {
    NSLog(@"添加滤镜");
    
    
    // 设置要添加滤镜的图片
    if(self.inputImage == nil){
        self.inputImage = [UIImage imageNamed:@"1"];
    }
    
    // 初始化要添加滤镜的图片
    GPUImagePicture *imageSorce = [[GPUImagePicture alloc] initWithImage:self.inputImage];
    
    // 创建一个滤镜
    GPUImageSketchFilter *imageFilter = [[GPUImageSketchFilter alloc] init];
    // 设置输出的图片的尺寸
    [imageFilter forceProcessingAtSize:CGSizeMake(640.0, 480.0)];
    // 添加滤镜
    [imageSorce addTarget:imageFilter];
    [imageFilter useNextFrameForImageCapture];
    [imageSorce processImage];
    
    // 获得添加滤镜后的图片
    self.filterImage = [imageFilter imageFromCurrentFramebuffer];
    
    // 显示添加滤镜后的图片
    [self.imageButton setImage:self.filterImage forState:UIControlStateNormal];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */


@end
